package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.business.domain.dto.DailyTrainGroupDTO;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.DailyTrainMapper;
import com.ruoyi.business.domain.DailyTrain;
import com.ruoyi.business.service.IDailyTrainService;

/**
 * 每日车次Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-06
 */
@Service
public class DailyTrainServiceImpl implements IDailyTrainService
{
    @Autowired
    private DailyTrainMapper dailyTrainMapper;

    /**
     * 查询每日车次
     * 
     * @param id 每日车次主键
     * @return 每日车次
     */
    @Override
    public DailyTrain selectDailyTrainById(Long id)
    {
        return dailyTrainMapper.selectDailyTrainById(id);
    }

    /**
     * 查询每日车次列表
     * 
     * @param dailyTrain 每日车次
     * @return 每日车次
     */
    @Override
    public List<DailyTrain> selectDailyTrainList(DailyTrain dailyTrain)
    {
        return dailyTrainMapper.selectDailyTrainList(dailyTrain);
    }

    /**
     * 新增每日车次
     * 
     * @param dailyTrain 每日车次
     * @return 结果
     */
    @Override
    public int insertDailyTrain(DailyTrain dailyTrain)
    {
        dailyTrain.setCreateTime(DateUtils.getNowDate());
        return dailyTrainMapper.insertDailyTrain(dailyTrain);
    }

    /**
     * 修改每日车次
     * 
     * @param dailyTrain 每日车次
     * @return 结果
     */
    @Override
    public int updateDailyTrain(DailyTrain dailyTrain)
    {
        dailyTrain.setUpdateTime(DateUtils.getNowDate());
        return dailyTrainMapper.updateDailyTrain(dailyTrain);
    }

    /**
     * 批量删除每日车次
     * 
     * @param ids 需要删除的每日车次主键
     * @return 结果
     */
    @Override
    public int deleteDailyTrainByIds(Long[] ids)
    {
        return dailyTrainMapper.deleteDailyTrainByIds(ids);
    }

    /**
     * 删除每日车次信息
     * 
     * @param id 每日车次主键
     * @return 结果
     */
    @Override
    public int deleteDailyTrainById(Long id)
    {
        return dailyTrainMapper.deleteDailyTrainById(id);
    }

    /**
     * 批量新增每日车
     * @param dailyTrainList 需要删除的数据主键集合
     * @return 结果
     */
    @Override
    public int insertDailyTrains(List<DailyTrain> dailyTrainList) {
        return dailyTrainMapper.insertDailyTrains(dailyTrainList);
    }
    @Override
    public List<DailyTrainGroupDTO> selectDailyTrainGroupByCode(DailyTrain dailyTrain) {
        // 获取原始列表数据
        List<DailyTrain> allTrains = dailyTrainMapper.selectDailyTrainList(dailyTrain);
        
        // 按code分组
        Map<String, List<DailyTrain>> trainGroups = allTrains.stream()
                .collect(Collectors.groupingBy(DailyTrain::getCode));
        
        // 转换为DTO
        List<DailyTrainGroupDTO> result = new ArrayList<>();
        for (Map.Entry<String, List<DailyTrain>> entry : trainGroups.entrySet()) {
            String code = entry.getKey();
            List<DailyTrain> trains = entry.getValue();
            
            if (!trains.isEmpty()) {
                DailyTrainGroupDTO groupDTO = new DailyTrainGroupDTO();
                groupDTO.setCode(code);
                groupDTO.setTrains(trains);
                
                // 获取发站和终点站信息（使用第一个车次的信息）
                DailyTrain firstTrain = trains.get(0);
                groupDTO.setType(firstTrain.getType());
                groupDTO.setStart(firstTrain.getStart());
                groupDTO.setEnd(firstTrain.getEnd());
                
                result.add(groupDTO);
            }
        }
        
        return result;
    }
    
    @Override
    public List<String> selectDistinctTrainCodes(DailyTrain dailyTrain, int pageSize, int offset) {
        return dailyTrainMapper.selectDistinctTrainCodes(dailyTrain, pageSize, offset);
    }
    
    @Override
    public int countDistinctTrainCodes(DailyTrain dailyTrain) {
        return dailyTrainMapper.countDistinctTrainCodes(dailyTrain);
    }
    
    @Override
    public List<DailyTrain> selectDailyTrainByTrainCodes(DailyTrain dailyTrain) {
        return dailyTrainMapper.selectDailyTrainByTrainCodes(dailyTrain);
    }
}
